package org.administer.contaroller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.administer.dao.ChatDao;
import org.administer.dao.CrmRoleDao;
import org.administer.pojo.CrmChat;
import org.administer.pojo.CrmRole;
import org.administer.pojo.CrmUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/xiaoshou")
public class XiaoshouController {
    @Autowired
    private ChatDao chatDao;
    @Autowired
    private CrmRoleDao crmRoleDao;
    @RequestMapping("/index")
    public String index(Map map) throws Exception{
        List<CrmRole> list =crmRoleDao.getAll();
        map.put("list",list);
        return "/xiaoshou/xiaoshoujihui";
    }
    @RequestMapping("/roleidddd")
    @ResponseBody
    public Map<String,Object> roleidddd(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit,Integer id) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Page<CrmUser> page11=PageHelper.startPage(page,limit);
        System.out.println("角色的daa"+id);
        List<CrmUser> roleid =crmRoleDao.roleiddd(id);
        map.put("code",0);
        map.put("msg",null);
        map.put("data",roleid);
        map.put("count",page11.getTotal());
        return map;
    }
    @RequestMapping("/neirong")
    @ResponseBody
    public Map<String,Object> neirong(HttpSession session,@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, CrmChat crmChat) throws Exception{
        Map<String,Object> map=new HashMap<>();
        //存储登录的用户
        CrmUser crmUser=(CrmUser) SecurityUtils.getSubject().getPrincipal();
        session.setAttribute("login_user",crmUser);
        System.out.println("用户编号"+crmUser.getId());
        CrmRole crmRole=crmRoleDao.getuserroleid(crmUser.getRoleid());
        session.setAttribute("crmRole",crmRole);
        crmChat.setUserid(crmRole.getId());
        if(crmChat.getUserid()==1){
            System.out.println("超管");
            Page<CrmChat> page1= PageHelper.startPage(page,limit);
            map.put("code",0);
            map.put("msg",null);
            map.put("data",chatDao.getAll());
            map.put("count",page1.getTotal());
            return  map;
        }
        else{
            System.out.println("普通");
            Page<CrmChat> page1= PageHelper.startPage(page,limit);
            map.put("code",0);
            map.put("msg",null);
            map.put("data",chatDao.aaa(crmUser.getId()));
            map.put("count",page1.getTotal());
        }
        return map;
    }
    @RequestMapping("/add")
    @ResponseBody
    @RequiresPermissions("xiaoshou:add")
    public Map<String,Object> add(HttpSession session,CrmChat crmChat) throws Exception{
        Map<String,Object> map=new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String time=sdf.format(new Date());
        CrmUser crmUser= (CrmUser) session.getAttribute("login_user");
        crmChat.setCreator(crmUser.getId());
        crmChat.setCreatorTime(time);
        chatDao.add(crmChat);
        return map;
    }
    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiresPermissions("xiaoshou:batchDelete")
    public Map<String,Object> batchDelete(Integer[] ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        chatDao.delete(ids);
        map.put("result",true);
        return map;
    }
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("xiaoshou:update")
    public Map<String,Object> update (CrmChat crmChat) throws Exception{
        Map<String,Object> map=new HashMap<>();
        chatDao.update(crmChat);
        return map;
    }
    @RequestMapping("/updatefenpei")
    @ResponseBody
    @RequiresPermissions("xiaoshou:updatefenpei")
    public Map<String,Object> updatefenpei (CrmChat crmChat) throws Exception{
        Map<String,Object> map=new HashMap<>();
        chatDao.updatefenpei(crmChat);
        return map;
    }
    @RequestMapping("/updatezhuangtai")
    @ResponseBody
    public Map<String,Object> updatazhuangtai(Integer id) throws Exception{
        Map<String,Object> map=new HashMap<>();
        chatDao.updatazhuangtai(id);
        return map;
    }
}
